<template>
  <div class="office-container">
    <!-- 使用iframe直接预览 -->
    <iframe
      :src="officeViewerUrl"
      class="office-viewer"
      frameborder="0"
      allowfullscreen
    ></iframe>
    
    <!-- 加载状态 -->
    <div v-if="loading" class="loading-overlay">
      <div class="loading-spinner"></div>
      <p>正在加载预览...</p>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';

const props = defineProps({
  officeViewerUrl: {
    type: String,
    required: true
  },
  fileUrl: {
    type: String,
    required: true
  },
  fileName: {
    type: String,
    default: '未命名文件'
  },
  fileContent: {
    type: String,
    default: ''
  }
});

const emit = defineEmits(['download']);

// 状态管理
const loading = ref(true);

// iframe加载完成后隐藏加载状态
const handleIframeLoad = () => {
  loading.value = false;
};

// 设置超时，确保加载状态不会一直显示
onMounted(() => {
  // 添加iframe加载事件监听
  const iframe = document.querySelector('.office-viewer');
  if (iframe) {
    iframe.addEventListener('load', handleIframeLoad);
  }
  
  // 设置超时，最多显示5秒加载状态
  setTimeout(() => {
    loading.value = false;
  }, 5000);
});
</script>

<style scoped>
.office-container {
  height: 100%;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  background-color: #f0f0f0;
  position: relative;
}

.office-viewer {
  width: 100%;
  height: 100%;
  border: none;
}

.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(255, 255, 255, 0.8);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 10;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 10px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

@media (max-width: 768px) {
  .office-container {
    min-height: 70vh;
  }
}
</style> 